using UnityEngine;
using System.Collections;
using System.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;


public class SATextureConfig : Singleton< SATextureConfig > 
{
	public Dictionary< int , SATexture > textureDic = new Dictionary< int , SATexture >();
	public Dictionary< int , SATexture > mapTextureDic = new Dictionary< int , SATexture >();


	void Awake ()
	{
		if ( mInstance == null )
		{
			mInstance = this;
			
			// init here ,
			
			initConfig();
		}
		
		//4574
	}

	
	public SATexture getTerrainTexture( int t )
	{
		if ( !mapTextureDic.ContainsKey( t ) ) 
		{
			return null;
		}
		
		return mapTextureDic[ t ];
	}

	public SATexture getTexture( int t )
	{
		if ( !textureDic.ContainsKey( t ) ) 
		{
			return null;
		}
		
		return textureDic[ t ];
	}
	
	
	public void setTexture( int t , SATexture sa )
	{
		textureDic[ t ] = sa;
	}
	
	
	public void removeUnuse()
	{
		
		
	}

	public void clear()
	{
		textureDic.Clear();
	}

	public void load( string path )
	{
		FileStream stream = File.Open( GameSetting.StreamingAssetsPath + path + ".sa" , FileMode.Open , FileAccess.Read );

		byte[] buff = new byte[ 32 ];


		int count = 0;
		while ( stream.Position < stream.Length ) 
		{
			count++;
			SATexture texture = new SATexture();

			int read = stream.Read( buff , 0 , 19 );

			texture.ID = BitConverter.ToInt32( buff , 0 );

			texture.px = BitConverter.ToInt16( buff , 4 );
			texture.py = BitConverter.ToInt16( buff , 6 );

			texture.w = BitConverter.ToInt16( buff , 8 );
			texture.h = BitConverter.ToInt16( buff , 10 );

			texture.e = buff[ 12 ];
			texture.s = buff[ 13 ];
			texture.flag = buff[ 14 ];

			texture.terrainID = BitConverter.ToInt32( buff , 15 );

			textureDic.Add( texture.ID , texture );
		}


		foreach( SATexture texture in textureDic.Values )
		{
			if ( texture.terrainID > 0 )
			{
				if ( mapTextureDic.ContainsKey( texture.terrainID ) )
				{
					//Debug.LogWarning ( "texture.terrainID again" + texture.terrainID );
					continue;
				}

				mapTextureDic.Add( texture.terrainID , texture );
			}
		}
		
		stream.Close();
	}
	
	public void initConfig()
	{
		load( "img" );
	}
	
	public void clearConfig()
	{
	}
	
	
	
}


